/ leetcodesolve / 121. Best Time to Buy and Sell Stock.py
swadhinbiswas update leetcode · 1/30/2025
6cdab40
18 lines · 355 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from typing import List
class Solve:
    def maxProfit(self,prices:List[int])->int:
        maxProfit=0
        bestbuy=prices[0]
        for i in range(1,len(prices)):
            if prices[i]>bestbuy:
                maxProfit=max(maxProfit,prices[i]-bestbuy)

            bestbuy=min(maxProfit,prices[i])
            
        return maxProfit